package com.jeeni.tetrisproject;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class Game implements ApplicationListener, InputProcessor {
	
	SpriteBatch spriteBatch;
	Texture texBlock0, texBlock1;
	
	public final static String block0 = "images/block0.png";
	public final static String block1 = "images/block1.png";
	
	// Tetris-related
	private int nHeight;
	private TetrisTask tetrisTask;
	private TetrisSound tetrisSound;
	
	@Override
	public void create() {
		// Initialize In-Game Variables
		tetrisSound = new TetrisSound();
		tetrisTask = new TetrisTask();	
		tetrisTask.start();
		
		// Initialize Library-related
		Gdx.input.setInputProcessor(this);
		spriteBatch = new SpriteBatch();
		texBlock0 = new Texture(Gdx.files.internal(block0));
		texBlock1 = new Texture(Gdx.files.internal(block1));
	}

	@Override
	public void render() {
		// Render
		Gdx.graphics.getGL10().glClear(GL10.GL_COLOR_BUFFER_BIT);
		spriteBatch.begin();
		
		// Render all block states
		for(int y = 1; y < 21; y++)
		{
			for(int x = 1; x < 11; x++)
			{
				if(tetrisTask.renderBuffer[y][x] > 0)
				{
					spriteBatch.draw(texBlock1, x*10, nHeight - y*10 - 10);
				}else{
					spriteBatch.draw(texBlock0, x*10, nHeight - y*10 - 10);
				}
			} // for(x)
		} // for(y)
		
		spriteBatch.end();
	}
	
	@Override
	public void resize(int width, int height) {
		spriteBatch.getProjectionMatrix().setToOrtho2D(0, 0, width, height);
		nHeight = height;
	}

	@Override
	public boolean keyDown(int keycode) {
		// Key Control
		switch(tetrisTask.getTetrisState())
		{
		case STATE_READY:
			break;
		case STATE_INGAME:
			
			switch(keycode)
			{
			case Keys.LEFT:
				tetrisSound.playSaw();
				tetrisTask.moveTo(-1, 0);
				break;
			case Keys.RIGHT:
				tetrisSound.playSakura();
				tetrisTask.moveTo(1, 0);
				break;
			case Keys.DOWN:
				tetrisSound.playSakura();
				tetrisTask.moveTo(0, 1);
				break;
			case Keys.UP:
				tetrisSound.playPiano();
				tetrisTask.rotateTo();
				break;
			case Keys.SPACE:
				tetrisSound.playPiano();
				tetrisTask.downTo();
			}
			
			
			break;
		}
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyTyped(char character) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDown(int x, int y, int pointer, int button) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchUp(int x, int y, int pointer, int button) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDragged(int x, int y, int pointer) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchMoved(int x, int y) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean scrolled(int amount) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
	}
	@Override
	public void pause() {
		// TODO Auto-generated method stub
	}
	@Override
	public void dispose() {
		tetrisTask.breakThis = true;
	}
}
